/**
 * Created by jackliu on 15/12/16.
 */

var common = require('../../common');
var pool = require('../index');
var mysql = require('mysql');
var ItbBaseModel = require('./ItbBaseModel');


// Schema 结构
var TgMapDocModel = function(obj){
    ItbBaseModel.call(this,obj);

  // 以下为表定义数据，需要的字段就填入
  //`map_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '分页映射id',
  //`tg_id` bigint(20) unsigned NOT NULL COMMENT '来源提纲id',
  //`map_name` varchar(100) DEFAULT NULL COMMENT '模版名',
  //`user_id` bigint(20) NOT NULL COMMENT '用户id',
  //`user_type` int(1) NOT NULL DEFAULT '0' COMMENT '0后台',
  //`publish` int(1) NOT NULL DEFAULT '0' COMMENT '0制作中; 1已完成',
  //`data` mediumtext COMMENT '内容（除page以外的信息）',
  //`hash_tag` varchar(100) DEFAULT NULL COMMENT '标签',
  //`size_id` bigint(20) NOT NULL COMMENT '尺寸',
  //`type_id` bigint(20) NOT NULL COMMENT '分类',
  //`order` int(3) NOT NULL DEFAULT '999' COMMENT '显示顺序',
  //`ver` int(10) NOT NULL DEFAULT '1' COMMENT '版本号',
  //`create_time` datetime NOT NULL,
  //`update_time` datetime NOT NULL,
  //`update_by` varchar(50) NOT NULL DEFAULT '',
  //`del_flg` int(1) NOT NULL DEFAULT '0',


};
TgMapDocModel.prototype = new ItbBaseModel();

// 取得表名
TgMapDocModel.prototype.getTableName = function(){
    return 'tg_map_doc';
}
// 取得查询语句中where 后面匹配主键的条件（需要各派生类进行重载）
TgMapDocModel.prototype.getWhereConditionOfKeyEqual = function(){
    return 'map_id='+this.map_id;
}

//------------------------------------------------------------------
/* 获取分页doc信息
 * 参数:map_id
 * 参数:callback:回调
 * */
TgMapDocModel.prototype.getMapDocInfoById = function(callback){
    var THIS = this;
    var tableName = this.getTableName();

    pool.pool.getConnection(function(err, connection) {
        // Use the connection
        var sql = ' select t.map_id, t.tg_id, t.map_name, t.user_id, t.publish, '+
            ' t.data, t.hash_tag, t.type_id, t.order, t.ver, '+
            ' t.size_id, s.width, s.height, p.type_name '+
            ' from TG_MAP_DOC t, TG_SIZE s, TG_TYPE p '+
            ' where t.del_flg=0 '+
            ' AND s.del_flg=0 AND s.size_id=t.size_id '+
            ' AND p.del_flg=0 AND p.type_id=t.type_id '+
            ' and t.map_id = '+ THIS.map_id +
            ' order by t.order, t.map_id DESC ';

        //var sqlValue = [THIS.type_id];
        //sql = mysql.format(sql, sqlValue);
        console.log(sql);
        connection.query(sql, function(error, result) {
            // And done with the connection.
            connection.release();
            //console.log();

            callback(error, result);
        });
    });
}
//--------------------------------end-------------------------------

//------------------------------------------------------------------
/* 获取分页doc信息
 * 参数:map_id
 * 参数:callback:回调
 * */
TgMapDocModel.prototype.getLatestMapDocInfoByType = function(callback){
    var THIS = this;
    var tableName = this.getTableName();

    pool.pool.getConnection(function(err, connection) {
        // Use the connection
        var sql = ' SELECT tt.* FROM (select t.map_id, t.tg_id, t.map_name, t.user_id, t.publish, '+
            ' t.data, t.hash_tag, t.type_id, t.size_id, t.order, t.ver, p.type_name , s.width AS w, s.height AS h '+
            ' from TG_MAP_DOC t, TG_TYPE p , TG_OUTLINE o , tg_size s'+
            ' where t.del_flg=0 AND t.publish=1'+
            ' AND t.type_id = '+ THIS.type_id +
            ' AND p.del_flg=0 AND p.type_id=t.type_id '+
            ' AND t.size_id = s.size_id '+
            ' AND o.tg_id=t.tg_id '+
            ' AND o.type_id=t.type_id '+
            //' AND o.fast=1 '+
            ' AND o.del_flg=0 '+
            ' AND o.publish=1 '+
            ' order by t.update_time DESC )tt LIMIT 1';

        ''

        //var sqlValue = [THIS.type_id];
        //sql = mysql.format(sql, sqlValue);
        console.log(sql);
        connection.query(sql, function(error, result) {
            // And done with the connection.
            connection.release();
            //console.log();

            callback(error, result);
        });
    });
}
//--------------------------------end-------------------------------


// 导出
module.exports = TgMapDocModel;